package com.miage.pki.form;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class CertificateCreate {
    String alias, type, datebefore, dateafter, algo;

    public String getAlias() {
        return alias;
    }

    public void setAlias(String alias) {
        this.alias = alias.trim();
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
    
    public String getDatebefore() {
        return datebefore;
    }

    public void setDatebefore(String datebefore) {
        this.datebefore = datebefore;
    }

    public String getDateafter() {
        return dateafter;
    }

    public void setDateafter(String dateafter) {
        this.dateafter = dateafter;
    }

    public String getAlgo() {
        return algo;
    }

    public void setAlgo(String algo) {
        this.algo = algo;
    }
    
    public boolean isEmpty(){
        return alias == null && type == null && datebefore == null && dateafter == null && algo == null;
    }
    
    public boolean isComplete(){
        return (alias != null && type != null && datebefore != null && dateafter != null && algo != null) && !alias.isEmpty() && !type.isEmpty() && !datebefore.isEmpty() && !dateafter.isEmpty() && !algo.isEmpty();
    }
    
    public boolean aliasOK(){
        return alias.matches("[a-z0-9 ]{1,24}") && !alias.matches("[ ]*");
    }
    
    public boolean dateOK(){
        try{
            Date b = new SimpleDateFormat("d/M/y", Locale.FRANCE).parse(datebefore);
            Date a = new SimpleDateFormat("d/M/y", Locale.FRANCE).parse(dateafter);
            Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            c.set(Calendar.HOUR_OF_DAY, 0);
            c.set(Calendar.MINUTE, 0);
            c.set(Calendar.SECOND, 0);
            c.set(Calendar.MILLISECOND, 0);

            if(b.before(a) && !b.before(c.getTime())){
                return true;
            }
        }catch(Exception e){}
        return false;
    }
    
}
